package com.fausto.learnimprint.core.service.impl

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl
import com.fausto.learnimprint.core.mapper.ReplyMapper
import com.fausto.learnimprint.core.model.entity.Reply
import com.fausto.learnimprint.core.service.IBlogService
import com.fausto.learnimprint.core.service.IDiscussService
import com.fausto.learnimprint.core.service.IReplyService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import java.time.LocalDateTime

/**
 *
 *
 * 服务实现类
 *
 *
 * @author fausto
 * @since 2022-03-28
 */
@Service
class ReplyServiceImpl : ServiceImpl<ReplyMapper, Reply>(), IReplyService{

    @Autowired
    lateinit var userService: UserService
    @Autowired
    lateinit var discussService: IDiscussService
    @Autowired
    lateinit var replyMapper: ReplyMapper
    @Autowired
    lateinit var blogService: IBlogService

    /**
     *      新增回复
     */
    @Transactional(rollbackFor = [Exception::class])
    override fun saveReply(discussId: Int, replyBody: String): Boolean {
        val user = userService.findUserFromDatabase()
        val discuss = discussService.findDiscussById(discussId)
        //保存回复至数据库
        val res = replyMapper.insert(Reply().apply {
            this.replyBody = replyBody
            this.discussId = discussId
            this.userId = user.userId
            this.replyTime = LocalDateTime.now()
        })
        //更新博文评论
        val blog = blogService.findBlogById(discuss.blogId)
        blog.blogDiscussCount += 1
        val result = blogService.updateById(blog)

        return result && res >0
    }

    /**
     *      用户删除回复
     */
    @Transactional(rollbackFor = [Exception::class])
    override fun deleteReply(replyId: Int): Boolean {
        val user = userService.findUserFromDatabase()
        val reply = replyMapper.selectById(replyId) ?: throw RuntimeException("回复不存在")
        //非回复作者,无权删除
        if (user.userId != reply.userId) throw RuntimeException("无权删除")
        //删除回复
        val res = replyMapper.deleteById(replyId)
        //由评论查询博客
        val discuss = discussService.findDiscussById(reply.discussId)
        val blog = blogService.findBlogById(discuss.blogId)
        //更新博客评论数
        blog.blogDiscussCount -= 1
        val result = blogService.updateById(blog)

        return res > 0 && result
    }

    /**
     *      管理员删除回复
     */
    @Transactional(rollbackFor = [Exception::class])
    override fun adminDelete(replyId: Int): Boolean {
        val reply = replyMapper.selectById(replyId) ?: throw RuntimeException("回复不存在")
        //删除回复
        val res = replyMapper.deleteById(reply)
        //查询评论
        val discuss = discussService.findDiscussById(reply.discussId)
        //更新博客评论数
        val blog = blogService.findBlogById(discuss.blogId)
        blog.blogDiscussCount -= 1
        val result = blogService.updateById(blog)

        return res > 0 && result
    }

}